﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class SMSLog : YJ.Data.Interface.ISMSLog
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public SMSLog()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.SMSLog实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.SMSLog model)
        {
            string sql = @"INSERT INTO smslog
				(ID,MobileNumber,Contents,SendUserID,SendUserName,SendTime,Status,Note) 
				VALUES(@ID,@MobileNumber,@Contents,@SendUserID,@SendUserName,@SendTime,@Status,@Note)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@MobileNumber", MySqlDbType.LongText, -1){ Value = model.MobileNumber },
				new MySqlParameter("@Contents", MySqlDbType.VarChar, 200){ Value = model.Contents },
				model.SendUserID == null ? new MySqlParameter("@SendUserID", MySqlDbType.VarChar, 36) { Value = DBNull.Value } : new MySqlParameter("@SendUserID", MySqlDbType.VarChar, 36) { Value = model.SendUserID },
				model.SendUserName == null ? new MySqlParameter("@SendUserName", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@SendUserName", MySqlDbType.Text, -1) { Value = model.SendUserName },
				new MySqlParameter("@SendTime", MySqlDbType.DateTime, -1){ Value = model.SendTime },
				new MySqlParameter("@Status", MySqlDbType.Int32, 11){ Value = model.Status },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = model.Note }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.SMSLog实体类</param>
        public int Update(YJ.Data.Model.SMSLog model)
        {
            string sql = @"UPDATE smslog SET 
				MobileNumber=@MobileNumber,Contents=@Contents,SendUserID=@SendUserID,SendUserName=@SendUserName,SendTime=@SendTime,Status=@Status,Note=@Note
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@MobileNumber", MySqlDbType.LongText, -1){ Value = model.MobileNumber },
				new MySqlParameter("@Contents", MySqlDbType.VarChar, 200){ Value = model.Contents },
				model.SendUserID == null ? new MySqlParameter("@SendUserID", MySqlDbType.VarChar, 36) { Value = DBNull.Value } : new MySqlParameter("@SendUserID", MySqlDbType.VarChar, 36) { Value = model.SendUserID },
				model.SendUserName == null ? new MySqlParameter("@SendUserName", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@SendUserName", MySqlDbType.Text, -1) { Value = model.SendUserName },
				new MySqlParameter("@SendTime", MySqlDbType.DateTime, -1){ Value = model.SendTime },
				new MySqlParameter("@Status", MySqlDbType.Int32, 11){ Value = model.Status },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = model.Note },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM smslog WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.SMSLog> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.SMSLog> List = new List<YJ.Data.Model.SMSLog>();
            YJ.Data.Model.SMSLog model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.SMSLog();
                model.ID = dataReader.GetString(0).ToGuid();
                model.MobileNumber = dataReader.GetString(1);
                model.Contents = dataReader.GetString(2);
                if (!dataReader.IsDBNull(3))
                    model.SendUserID = dataReader.GetString(3).ToGuid();
                if (!dataReader.IsDBNull(4))
                    model.SendUserName = dataReader.GetString(4);
                model.SendTime = dataReader.GetDateTime(5);
                model.Status = dataReader.GetInt32(6);
                if (!dataReader.IsDBNull(7))
                    model.Note = dataReader.GetString(7);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.SMSLog> GetAll()
        {
            string sql = "SELECT * FROM smslog";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.SMSLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM smslog";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.SMSLog Get(Guid id)
        {
            string sql = "SELECT * FROM smslog WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.SMSLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }
    }
}